package com.group.project.hrms.controller;

import com.group.project.hrms.entity.DingAuthInfo;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.IDingAuthInfoService;
import com.group.project.hrms.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class DingAuthInfoController {

    @Autowired
    private IDingAuthInfoService iDingAuthInfoService;

    @Autowired
    private LogUtil logUtil;


    /**
     * 获取钉钉的认证状态（此系统必须先配置预先创建的钉钉小程序参数，才可与钉钉同步）
     * @return
     */
    @RequestMapping("/basic/doCheckDingAuthStatus")
    @ResponseBody
    public ResultEntity doCheckDingAuthStatus(HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        List<DingAuthInfo> list = iDingAuthInfoService.selectAllDingAuthInfo();
        if(null != list && list.size() == 0){
            resultEntity.setCode("F");
            resultEntity.setMsg("Fail");
            return resultEntity;
        }
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        resultEntity.setDataList(list);
        return resultEntity;
    }


    /**
     * 向后台数据库插入钉钉认证信息
     * @param map 前端传来的数据集合
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/insertDingAuthInfo")
    @ResponseBody
    public ResultEntity insertDingAuthInfo(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Object agentId = map.get("AgentId");
        Object miniAppId = map.get("MiniAppId");
        Object appKey = map.get("AppKey");
        Object appSecret = map.get("AppSecret");
        if(null != agentId && null != miniAppId && null != appKey && null != appSecret){
            List<DingAuthInfo> dingAuthInfoList = iDingAuthInfoService.selectAllDingAuthInfo();
            if(null != dingAuthInfoList && dingAuthInfoList.size() == 0){
                DingAuthInfo dingAuthInfo = new DingAuthInfo();
                dingAuthInfo.setcTime(new Date());
                dingAuthInfo.setAgentId(agentId.toString());
                dingAuthInfo.setMiniAppId(miniAppId.toString());
                dingAuthInfo.setAppKey(appKey.toString());
                dingAuthInfo.setAppSecret(appSecret.toString());
                iDingAuthInfoService.insertDingAuthInfo(dingAuthInfo); //如果不存在就插入数据
            }else {
                DingAuthInfo dingAuthInfoBefore = dingAuthInfoList.get(0);//获取数据库中的第一条记录（目前版本默认只能配置一个小程序的钉钉认证）
                dingAuthInfoBefore.setmTime(new Date());
                dingAuthInfoBefore.setAgentId(agentId.toString());
                dingAuthInfoBefore.setMiniAppId(miniAppId.toString());
                dingAuthInfoBefore.setAppKey(appKey.toString());
                dingAuthInfoBefore.setAppSecret(appSecret.toString());
                iDingAuthInfoService.updateDingAuthInfo(dingAuthInfoBefore); //如果存在，就更新数据
            }
            logUtil.insertSystemEventLog(staffName.toString(), new Date(), "设置了钉钉认证参数信息", "系统");
            resultEntity.setCode("S");
            resultEntity.setDataList(iDingAuthInfoService.selectAllDingAuthInfo());
            resultEntity.setMsg("Success");
            return resultEntity;
        }else {
            resultEntity.setCode("F");
            resultEntity.setMsg("Fail");
            return resultEntity;
        }
    }

}
